﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.ASPxUploadControl;

namespace ElingOA6_Admin_DSJCJ.Common
{
    /// <summary>
    /// 上传控件辅助类
    /// </summary>
    public class UploadControlHelper
    {
        /// <summary>
        /// 图片类验证
        /// </summary>
        public static readonly ValidationSettings PicValidationSettings = new ValidationSettings
        {
            AllowedFileExtensions = new string[] { ".jpg", ".jpeg", ".jpe", ".gif" },
            MaxFileSize = 20971520
        };


        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fu">fileUpload控件</param>
        /// <param name="url">../abc/image</param>
        /// <returns></returns>
        public static string Upload(System.Web.UI.WebControls.FileUpload fu, string url)
        {
            if (fu.HasFile == false || fu.PostedFile.ContentLength <= 0)
            {
                return "1";//文件内容为空
            }

            //获取文件类型、大小、名称

            string type = fu.PostedFile.ContentType;
            int size = fu.PostedFile.ContentLength;
            string factFileName = Path.GetFileNameWithoutExtension(fu.FileName);

            if (type != "image/bmp" && type != "image/gif" && type != "image/pjpeg" && type != "image/x-png" && type != "image/jpg")
            {
                return "2";//上传类型不是图片
            }

            if (size > 10 * 1024 * 1024)
            {
                return "3";//超出大小10M
            }

            //获取文件的扩展名
            string extension = Path.GetExtension(fu.PostedFile.FileName).ToLower();

            //改变图片名称
            string strnewFileNmae = GenerateUtil.GenerateStringId();

            //判断图片保存文件夹是否存在

            GenerateUtil.addfolder(url);

            string fullpath = HttpContext.Current.Server.MapPath(url) + "/" + strnewFileNmae + extension;

            //判断是否存在相同的用户名
            if (File.Exists(fullpath) == true)
            {
                return "4";//文件已存在

            }

            //上传文件到硬盘

            fu.SaveAs(fullpath);
            return strnewFileNmae + extension;//文件名称
        }


        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fu">fileUpload控件</param>
        /// <param name="url">../abc/image</param>
        /// <returns></returns>
        public static string Upload1(System.Web.UI.WebControls.FileUpload fu, string url)
        {
            if (fu.HasFile == false || fu.PostedFile.ContentLength <= 0)
            {
                return "1";//文件内容为空
            }

            //获取文件类型、大小、名称

            string type = fu.PostedFile.ContentType;
            int size = fu.PostedFile.ContentLength;
            string factFileName = Path.GetFileNameWithoutExtension(fu.FileName);
            //获取文件的扩展名
            string extension = Path.GetExtension(fu.PostedFile.FileName).ToLower();

            if (extension != ".jpg" && extension != ".gif" && extension != ".bmp")
            {
                return "2";//上传类型不是图片
            }

            if (size > 10 * 1024 * 1024)
            {
                return "3";//超出大小10M
            }



            //改变图片名称
            string strnewFileNmae = GenerateUtil.GenerateStringId();

            //判断图片保存文件夹是否存在

            GenerateUtil.addfolder(url);

            string fullpath = HttpContext.Current.Server.MapPath(url) + "/" + strnewFileNmae + extension;

            //判断是否存在相同的用户名
            if (File.Exists(fullpath) == true)
            {
                return "4";//文件已存在

            }

            //上传文件到硬盘

            fu.SaveAs(fullpath);
            return strnewFileNmae + extension;//文件名称
        }





        /// <summary>
        /// 上传图片或Flash
        /// </summary>
        /// <param name="fu">fileUpload控件</param>
        /// <param name="url">../abc/image</param>
        /// <returns></returns>
        public static string UploadFlashOrImg(System.Web.UI.WebControls.FileUpload fu, string url)
        {
            if (fu.HasFile == false || fu.PostedFile.ContentLength <= 0)
            {
                return "1";//文件内容为空
            }

            //获取文件类型、大小、名称

            string type = fu.PostedFile.ContentType;
            int size = fu.PostedFile.ContentLength;
            string factFileName = Path.GetFileNameWithoutExtension(fu.FileName);

            if (type != "image/bmp" && type != "image/gif" && type != "image/pjpeg" && type != "image/x-png" && type != "application/x-shockwave-flash")
            {
                return "2";//上传类型不是图片
            }

            if (size > 10 * 1024 * 1024)
            {
                return "3";//超出大小10M
            }

            //获取文件的扩展名
            string extension = Path.GetExtension(fu.PostedFile.FileName).ToLower();

            //改变图片名称
            string strnewFileNmae = GenerateUtil.GenerateStringId();

            //判断图片保存文件夹是否存在

            GenerateUtil.addfolder(url);

            string fullpath = HttpContext.Current.Server.MapPath(url) + "/" + strnewFileNmae + extension;

            //判断是否存在相同的用户名
            if (File.Exists(fullpath) == true)
            {
                return "4";//文件已存在

            }

            //上传文件到硬盘

            fu.SaveAs(fullpath);
            return strnewFileNmae + extension;//文件名称
        }




        //获取图片的二进制流

        public static byte[] GetImgByte(string strImgPath)
        {
            string path = HttpContext.Current.Server.MapPath(strImgPath);
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);

            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Jpeg);
            byte[] buffer = ms.ToArray();
            ms.Close();
            image.Dispose();
            return buffer;
        }

        //从一串字符串获取图片的名称

        public static string GetLoadImageName(string strImgPath)
        {
            string strImageName = "";
            int t = strImgPath.LastIndexOf("/");
            if (t == -1)
            {
                t = strImgPath.LastIndexOf("\\");
            }

            strImageName = strImgPath.Substring(t + 1);

            return strImageName;
        }

    }
}